import Header from '@/components/Header';
import { useEffect, useState } from 'react';
import { getUserInfo, User } from '@/services/users';
import Loading from '@/components/Loading';
import { IRouteComponentProps } from 'umi';

export function useUserInfo() {
  const [user, setUser] = useState<User>();
  useEffect(() => {
    getUserInfo().then(setUser);
  }, []);
  return user;
}

export default function(props: IRouteComponentProps) {
  // const match = useRouteMatch();
  // console.log(props);
  if (props.location.pathname === '/login') {
    return props.children;
  }
  const user = useUserInfo();
  if(!user) {
    return <Loading/>;
  }
  return (
      <>
        <Header title={props.location.pathname} />
        {props.children}
      </>
  );
}
